//
//  AppDelegate.m
//  Item A House
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "MoreViewController.h"
#import "MyViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "Singletion.h"
@interface AppDelegate ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *manager;

@end
//开场动画,定位弹出框
@implementation AppDelegate
- (void)dealloc
{
    [_tab release];
    [_manager release];
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-20, 0, self.window.frame.size.width+40, self.window.frame.size.height)];
    
    imageView.image = [UIImage imageNamed:@"welcome.jpg"];
    
    [self.window addSubview:imageView];
   
    
    
    
    UIImageView *imageLogo = [[UIImageView alloc] initWithFrame:CGRectMake((self.window.frame.size.width - 200) / 2, 150, 200, 200)];
    imageLogo.image = [UIImage imageNamed:@"house.png"];
    imageLogo.alpha = 0.8;
    [self.window addSubview:imageLogo];
    
    [UIView animateWithDuration:2 animations:^{
        imageView.center = CGPointMake(imageView.center.x+20, imageView.center.y);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:2 animations:^{
            
            imageView.center = CGPointMake(imageView.center.x-20, imageView.center.y);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:1 animations:^{
                imageView.alpha = 0;
                imageLogo.alpha = 0;
            } completion:^(BOOL finished) {
                [imageLogo removeFromSuperview];
                [imageView removeFromSuperview];
                self.manager = [[CLLocationManager alloc] init];
                self.manager.delegate = self;
                self.manager.desiredAccuracy = kCLLocationAccuracyBest;
                self.manager.distanceFilter = 100;
                [self.manager requestAlwaysAuthorization];
                [self.manager startUpdatingLocation];
                [_manager release];
            }];
            
        }];
        
    }];
    
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
    MainViewController *main = [[MainViewController alloc] init];
    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:main];
    [mainNav.navigationBar setBarTintColor:[UIColor brownColor]];
        
    main.title = @"购房网";
    UIImage *mainImage = [UIImage imageNamed:@"Main.png"];
    [main.tabBarItem setImage:mainImage];
    
    MyViewController *My = [[MyViewController alloc] init];
    UINavigationController *myNav = [[UINavigationController alloc] initWithRootViewController:My];
    [myNav.navigationBar setBarTintColor:[UIColor brownColor]];
    My.title = @"我的";
    UIImage *myImage = [UIImage imageNamed:@"My.png"];
    [My.tabBarItem setImage:myImage];
    
    MoreViewController *more = [[MoreViewController alloc] init];
    UINavigationController *moreNav = [[UINavigationController alloc] initWithRootViewController:more];
    [moreNav.navigationBar setBarTintColor:[UIColor brownColor]];
    more.title = @"更多";
    UIImage *moreImage = [UIImage imageNamed:@"More.png"];
    [more.tabBarItem setImage:moreImage];
    
    NSMutableArray *array = [NSMutableArray array];
    [array addObject:mainNav];
    [array addObject:myNav];
    [array addObject:moreNav];
    
    self.tab = [[UITabBarController alloc] init];
    self.tab.viewControllers = [NSMutableArray arrayWithArray:array];
    [_window setRootViewController:self.tab];
    

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-20, 0, self.window.frame.size.width+40, self.window.frame.size.height)];
    
    imageView.image = [UIImage imageNamed:@"welcome.jpg"];

    });

    return YES;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //开场动画时间
    CLLocation *cullLoaction = [locations lastObject];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"mergedcitylist" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *cityDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *cityArr = [cityDic objectForKey:@"city"];
    for (NSDictionary *smallDic in cityArr) {
        NSDictionary *ct = [smallDic objectForKey:@"ct"];
        if (cullLoaction.coordinate.longitude > [[[[[ct objectForKey:@"gmap_info"] objectForKey:@"shape"] objectAtIndex:0] objectAtIndex:0] floatValue] && cullLoaction.coordinate.longitude < [[[[[ct objectForKey:@"gmap_info"] objectForKey:@"shape"] objectAtIndex:1] objectAtIndex:0] floatValue] && cullLoaction.coordinate.latitude > [[[[[ct objectForKey:@"gmap_info"] objectForKey:@"shape"] objectAtIndex:2] objectAtIndex:1] floatValue] && cullLoaction.coordinate.latitude < [[[[[ct objectForKey:@"gmap_info"] objectForKey:@"shape"] objectAtIndex:0] objectAtIndex:1] floatValue]) {
            Singletion *city = [Singletion shareManager];
            city.cityid = [ct objectForKey:@"id"];
        }
    }
    [self.manager stopUpdatingLocation];
    //window的根除
    
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
